package com.huanying.goodjob.store;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;
import com.huanying.framework.adminuser.Adminuser;
import com.huanying.goodjob.advertisment.Advertisment;

@Service
public class StoreServiceImpl implements StoreService{

	@Autowired
	private BaseDao dao;
	
	@Override
	public void add_store(Store store) throws Exception {
		// TODO Auto-generated method stub
		dao.save(store);
	}

	@Override
	public void update_store(Store store) throws Exception {
		// TODO Auto-generated method stub
		dao.merge(store);
	}

	@Override
	public void delete_store(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Store set status=0 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
		dao.exculeByParams(hql, param);
	}

	@Override
	public Store get_store(int id) throws Exception {
		// TODO Auto-generated method stub
		return (Store)dao.load(Store.class, id);
	}

	@Override
	public PageBean search_store(Map<String, Object> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer("from Store as s where 1=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("user")!="" && map.get("user")!=null)
		{
			hql.append(" and s.user.id=?");
			param.add(((Adminuser)map.get("user")).getId());
			
		}
		if(map.get("shopname")!=null && map.get("shopname")!="")
		{
			hql.append(" and s.shopname like ?");
			param.add("%"+map.get("shopname")+"%");
		}
		if(map.get("name")!=null && map.get("name")!="")
		{
			hql.append(" and s.shopmanagename like ?");
			param.add("%"+map.get("name")+"%");
		}
		if(map.get("type")!=null && map.get("type")!="")
		{
			hql.append(" and s.type=?");
			param.add(Integer.parseInt((String)map.get("type")));
		}
		if(map.get("introduce")!=null && map.get("introduce")!="")
		{
			hql.append(" and s.introduce like ?");
			param.add("%"+map.get("introduce")+"%");
		}
		if(map.get("phone")!=null && map.get("phone")!="")
		{
			hql.append(" and s.phone like ?");
			param.add("%"+map.get("phone")+"%");
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
	    final int currentPage = PageBean.countCurrentPage(page);
	    List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);
	    
	    //把分页信息保存到Bean中
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
		
		return pageBean;
	}

	@Override
	public void update_storerelation(int store_id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Store set status=0 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(store_id);
		dao.exculeByParams(hql, param);
	}

	@Override
	public List<Store> front_get_store(Map<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer("from Store as s where s.status=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("province_code")!=null && map.get("province_code")!="")
		{
			hql.append(" and s.province.code=?");
			param.add((String)map.get("province_code"));
		}
		if(map.get("city_code")!=null && map.get("city_code")!="")
		{
			hql.append(" and s.city.code=?");
			param.add((String)map.get("city_code"));
			}
		if(map.get("county_code")!=null && map.get("county_code")!="")
		{
			hql.append(" and s.county.code=?");
			param.add(map.get("county_code"));
		}
		return (List<Store>)dao.exculeHqlByParams(hql.toString(), param);
	}

	
}
